package com.ansion.utils.permission;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import com.ansion.basetool.R;

public class NoPermissionDialog extends DialogFragment {

    public static NoPermissionDialog newInstance(int icon, String guide_txt,boolean primeStyle) {
        NoPermissionDialog dialog = new NoPermissionDialog();

        Bundle args = new Bundle();
        args.putInt("permission_icon", icon);
        args.putString("guide_txt",guide_txt);
        args.putBoolean("prime_style", primeStyle);
        dialog.setArguments(args);

        return dialog;
    }


    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        if (window!=null) {
            WindowManager.LayoutParams windowParams = window.getAttributes();
            windowParams.dimAmount = 0.80f;
            windowParams.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
            window.setAttributes(windowParams);
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        boolean primeStyle = false;
        if (getArguments()!=null) {
            primeStyle = getArguments().getBoolean("prime_style", false);
        }

        int layoutId = R.layout.permission_pre_dialog;
        if (primeStyle){
            layoutId = R.layout.permission_prime_dialog;
        }

        View v = inflater.inflate(layoutId, container, false);
        ImageView imageView = v.findViewById(R.id.img_permission_icon);

        TextView textView = v.findViewById(R.id.txt_permission_notice);

        int image_id = getArguments().getInt("permission_icon");
        String guide_txt = getArguments().getString("guide_txt");

        if (!primeStyle) {
            imageView.setImageResource(image_id);
        }

        textView.setText(guide_txt);


        Button btnSetting = v.findViewById(R.id.btn_settings);
        btnSetting.setVisibility(View.VISIBLE);
        btnSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity activity = getActivity();

                if (activity!=null) {
                    try {
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
//                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
                        intent.setData(uri);
                        activity.startActivityForResult(intent, 43);
                    } catch (Throwable th) {
                        th.printStackTrace();
                    }
                }

                NoPermissionDialog.this.dismiss();
            }
        });

        Button btnNotNow = v.findViewById(R.id.btn_not_now);
        btnNotNow.setVisibility(View.VISIBLE);
        btnNotNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Activity activity = getActivity();
                NoPermissionDialog.this.dismiss();
                if (activity != null) {
                    if (activity instanceof PermissionShadowActivity){
                        ((PermissionShadowActivity) activity).notNow();
                    }
//                    activity.finish();
                }
            }
        });

        return v;
    }
}
